package site.wuao.simplerxjava.operator;

import android.util.Log;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * 操作符 - 布尔
 *
 * @author wuao
 * @date 2017.12.08
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class BooleanOperator {
    /** 标记 */
    private static final String TAG = "BooleanOperator";

    /**
     * all
     *
     * @note 是否所有的数据满足判断条件，如果是返回true，如果不是返回false
     */
    public void all() {
        Observable
                .just(1, 2, 3, 4, 5)
                .all(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                        return integer < 3;
                    }
                })
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        Log.i(TAG, "call: " + aBoolean);
                    }
                });

        // call: 1
        // call: 2
        // call: 3
        // call: false
    }

    /**
     * exists - 存在
     *
     * @note 是否存在数据满足判断条件，如果是返回true，如果不是返回false
     */
    public void exists() {
        Observable
                .just(1,2,3,4,5)
                .exists(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                        return integer < 3;
                    }
                })
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        Log.i(TAG, "call: " + aBoolean);
                    }
                });

        // call: 1
        // call: true
    }

    /**
     * contains - 包含
     */
    public void contains() {
        Observable
                .just(1, 2, 3, 4, 5)
                .contains(1)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        Log.i(TAG, "call: " + aBoolean);
                    }
                });

        // call: true
    }

    /**
     * isEmpty - 是否为空
     */
    public void isEmpty() {
        Observable
                .just(1, 2, 3, 4, 5)
                .isEmpty()
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        Log.i(TAG, "call: " + aBoolean);
                    }
                });

        // call: false
    }

    /**
     * sequenceEqual
     *
     * @note 1.两个Observable发射的数据是否完全一致，如果是返回true，如果不是返回false
     * 2.相同的数据，相同的顺序
     */
    public void sequenceEqual() {
        Observable
                .sequenceEqual(Observable.just(1,2,3), Observable.just(1,2,3))
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        Log.i(TAG, "call: " + aBoolean);
                    }
                });

        // call: true
    }
}
